﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Computer_Manager.DTO;

namespace Computer_Manager.DAL
{
    class DAL_user
    {
        DbProvider db = new DbProvider();
        SqlCommand cmd;

        public void user_insert(DTO_user dt)
        {
            db.Open();
            cmd = new SqlCommand("user_Insert", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = dt.username;
            cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = dt.password;
            cmd.Parameters.Add("@fullname", SqlDbType.NVarChar).Value = dt.fullname;
            cmd.Parameters.Add("@gender", SqlDbType.Bit).Value = dt.gender;
            cmd.Parameters.Add("@role_id", SqlDbType.Int).Value = dt.role_id;

            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }
        public void user_Update(DTO_user dt)
        {
            db.Open();
            cmd = new SqlCommand("user_Update", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_user", SqlDbType.Int).Value = dt.id_user;
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = dt.username;
            cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = dt.password;
            cmd.Parameters.Add("@fullname", SqlDbType.NVarChar).Value = dt.fullname;
            cmd.Parameters.Add("@gender", SqlDbType.Bit).Value = dt.gender;
            cmd.Parameters.Add("@role_id", SqlDbType.Int).Value = dt.role_id;

            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }
        public void user_ChangePass(DTO_user dt)
        {
            db.Open();
            cmd = new SqlCommand("user_ChangePass", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_user", SqlDbType.Int).Value = dt.id_user;
            cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = dt.password;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }
        public void user_Delete(DTO_user dt)
        {
            db.Open();
            cmd = new SqlCommand("user_Delete", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_user", SqlDbType.Int).Value = dt.id_user;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            db.getConn().Close();
        }
        public DataTable GetAll()
        {
            SqlDataAdapter adapter = new SqlDataAdapter("user_SelectAll", db.getConn());
            DataTable table = new DataTable();
            adapter.Fill(table);
            adapter.Dispose();
            return table;
        }
        public DataTable GetByID(DTO_user dt)
        {
            cmd = new SqlCommand("user_GetByID", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_user", SqlDbType.Int).Value = dt.id_user;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }
        public DataTable CheckLogin(DTO_user dt)
        {
            cmd = new SqlCommand("user_CheckLogin", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = dt.username;
            cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = dt.password;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public DataTable GetByUserName(DTO_user dt)
        {
            cmd = new SqlCommand("user_GetByUserName", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = dt.username;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }
    }
}
